$(document).ready(function(){
	$('#picture_form').submit(function(){
		return false;
	});
	$('#btn_search_product').click(function(){
		getProduct();
	});
	$("#input_file1").change(function(){
		readURL(this, 1);
	});
	$("#input_file2").change(function(){
		readURL(this, 2);
	});
	$("#input_file3").change(function(){
		readURL(this, 3);
	});
	$("#input_file4").change(function(){
		readURL(this, 4);
	});
	$("#input_file5").change(function(){
		readURL(this, 5);
	});
	$("#input_file6").change(function(){
		readURL(this, 6);
	});
});

function readURL(input, position) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            $('#review_image_' + position).attr('src', e.target.result);
			addImage(getFormData(position));
        }
        reader.readAsDataURL(input.files[0]);
    }
}

function removeImage(position){
	var target = document.getElementById('input_file'+position);
	target.value = target.defaultValue;
	$('#review_image_' + position).attr('src', '');
	var product_id = $('#product_detail_id').val();
	if(product_id != 0){
		var data = {
			ordinal: position,
			product_id: product_id
		};
		$.ajax({
			url: base_url + DELETE_PRODUCT_IMAGE,
			data: data,
			type: 'POST',
			success: function(res){
				$('#submit_error').html('Thành công');
			},
			error: function(err){
				$('#submit_error').html('Thất bại');
			}
		});
	}
}

function uploadImage1(){
	$('#input_file1').click();
}

function uploadImage2(){
	$('#input_file2').click();
}

function uploadImage3(){
	$('#input_file3').click();
}

function uploadImage4(){
	$('#input_file4').click();
}

function uploadImage5(){
	$('#input_file5').click();
}

function uploadImage6(){
	$('#input_file6').click();
}

function getProduct(){
	var search_product_code = $('#search_product_code').val();
	var search_product_custom_code = $('#search_product_custom_code').val();
	$('#submit_error').html('');
	if(search_product_code !== '' && search_product_custom_code !== ''){
		var data = {
			product_code_id: search_product_code,
			code: search_product_custom_code
		};
		var request = {
			url: base_url + GET_PRODUCT_DETAIL,
			type: 'POST',
			data: data,
			success: function(data){
				if(data){
					var product = JSON.parse(data);
					$('#product_detail_id').val(product.id);
					getImage(product.id);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html('Dữ liệu nhập vào không hợp lệ');
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function getImage(product_id){
	if(product_id !== ''){
		var data = {
			product_id: product_id
		};
		var request = {
			url: base_url + GET_PRODUCT_IMAGE,
			type: 'POST',
			data: data,
			success: function(data){
				if(data){
					var productImage = JSON.parse(data);
					productImage.forEach(function(image){
						$('#review_image_' + image.ordinal).attr('src', image.image);
					});
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html('Dữ liệu nhập vào không hợp lệ');
	}
}

function getFormData(position){
	var formData = new FormData();
	formData.append("picture", $('#input_file' + position)[0].files[0]);
	formData.append('product_id', $('#product_detail_id').val());
	console.log($('#product_detail_id').val());
	formData.append('ordinal', position);
	return formData;
}

function addImage(formData){
	$.ajax({
		url: base_url + ADD_PRODUCT_IMAGE,
		data: formData,
		processData: false,
		contentType: false,
		type: 'POST',
		success: function(res){
			$('#submit_error').html('Thành công');
		},
		error: function(err){
			$('#submit_error').html('Thất bại');
		}
	});
}